//
//  HeadScrollView.m
//  图片故事
//
//  Created by 大帅逼 on 16/1/7.
//  Copyright © 2016年 helloApp. All rights reserved.
//

#import "HeadScrollView.h"
#import "Header.h"

@interface HeadScrollView ()<UIScrollViewDelegate>

@property(nonatomic,strong) UIScrollView        *scrollView;
@property(nonatomic,strong) NSTimer             *timer;             // 计时器
@property(nonatomic,strong) UIPageControl       *pageControl;

@end

@implementation HeadScrollView

+ (instancetype) shardHeadView {
    
    static HeadScrollView *headView = nil;
    
    static dispatch_once_t onceToken;
    
    dispatch_once(&onceToken, ^{
        
        headView = [[HeadScrollView alloc] init];
    });
    
    return headView;
}

- (instancetype)initWithFrame:(CGRect)frame images : (NSArray *) array {
    
    if (self = [super initWithFrame:frame]) {
        
        _images = array;
        // 初始化页面
        [self initView];
    }
    return self;
}

- (void) initView {
    
    [self setImageForScrollview];
    
    [self addSubview:self.scrollView];
    
    [self addSubview:self.pageControl];
}

#pragma mark -- 设置滚动视图的图片内容
- (void) setImageForScrollview {
    
    // 遍历数组并设置图片视图展示
    for (int i = 0; i < _images.count; i ++) {
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_W * i, 0, SCREEN_W, SCREEN_H - My_Height(200))];
        [imageView setTag:(200+i)];
        [imageView setImage:[UIImage imageNamed:[_images objectAtIndex:i]]];
        [self.scrollView addSubview:imageView];
    }
}

#pragma mark -- 滚动视图的代理方法
// 滚动时的代理回调方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    
    // 获得滚动时的偏移量
    CGFloat ofsetX = scrollView.contentOffset.x;
    
    // 计算当前显示图片页码
    NSInteger imagePage = ofsetX / SCREEN_W + 0.5;
    
    // 设置分页显示器的当前页码
    self.pageControl.currentPage = imagePage - 1;
}

// 动画滚动时
- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
    [self scrollViewDidEndDecelerating:scrollView];
}

// 滚动结束时
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    
    // 获得偏移量
    CGFloat ofsetX = scrollView.contentOffset.x;
    
    if (ofsetX > (_images.count - 2) * SCREEN_W) {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_W, 0)];
    }
    if (ofsetX < SCREEN_W) {
        [self.scrollView setContentOffset:CGPointMake(SCREEN_W * (_images.count - 2), 0)];
    }
}

//// 手动拖拽开始时
//- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
//    //停止定时器
//    [self stopTimer];
//}
//
//// 手动拖拽结束时
//-(void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
//    [self startTimer];
//}

#pragma mark --定时器方法
- (void) startTimer {
    _timer = [NSTimer timerWithTimeInterval:2.0 target:self selector:@selector(autoScrollImage) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
}

- (void) stopTimer {
    [_timer invalidate];
    _timer = nil;
}

- (void) autoScrollImage {
    
    CGFloat ofsetX = self.scrollView.contentOffset.x;
    
    // 设置偏移量
    [self.scrollView setContentOffset:CGPointMake(ofsetX + SCREEN_W, 0) animated:YES];
}


#pragma mark -- 懒加载

- (UIScrollView *)scrollView {
    
    if (!_scrollView) {
        
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H - My_Height(100))];
        [_scrollView setBackgroundColor:[UIColor colorWithWhite:0.9 alpha:1]];
        
        // 设置滚动视图可滑动大小
        [_scrollView setContentSize:CGSizeMake(SCREEN_W * _images.count, 0)];
        
        // 设置滚动视图的初始值
        [_scrollView setContentOffset:CGPointMake(SCREEN_W, 0)];
        
        // 隐藏滑动条
        [_scrollView setShowsHorizontalScrollIndicator:NO];
        
        // 设置代理
        [_scrollView setDelegate:self];
        
        // 设置以页滑动
        [_scrollView setPagingEnabled:YES];
    }
    
    return _scrollView;
}

- (UIPageControl *)pageControl {
    
    if (!_pageControl) {
        
        _pageControl = [[UIPageControl alloc] init];
        
        // 设置初始数据
        [_pageControl setNumberOfPages:3];
        [_pageControl setCurrentPage:0];
        
        [_pageControl setBounds:CGRectMake(0, 0, My_Width(100), My_Height(20))];
        [_pageControl setCenter:CGPointMake(SCREEN_W / 2, 30)];
    }
    return _pageControl;
}

@end
